package com.serkol.driving.learning.api;

import com.serkol.bean.MyPage;
import com.serkol.bean.Result;
import com.serkol.constant.ResultConstant;
import com.serkol.driving.learning.entity.LearningType;
import com.serkol.driving.learning.service.LearningTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("api/learningType")
@RestController
public class LearningTypeApiController {
    @Autowired
    private LearningTypeService service;
    @GetMapping("/findByList")
    public Result findByList(@RequestParam(name="pageSize", defaultValue = "10") int pageSize, @RequestParam(name="page", defaultValue = "0") int page){
        Result result = new Result();
        MyPage list = service.findByList(pageSize, page);
        if (list.getSize()>0) {
            result.setStatus(ResultConstant.SUCSESS);
            result.setData(list);
        } else {
            result.setStatus(ResultConstant.EMPTIY);
        }
        return result;
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody LearningType data) {
        boolean flag = false;
        if (data.getId() != null && data.getId() > 0) {
            flag = service.updateById(data);
        } else {
            flag = service.save(data);
        }
        if (flag) {
            return Result.ok();
        } else {
            return Result.failure();
        }

    }

//api/learningType/list
    @GetMapping("/list")
    public Result list() {
        Result result = new Result();
        List<Map<String,Object>> list = service.findByListMini();
        if (!list.isEmpty()) {
            result.setStatus(ResultConstant.SUCSESS);
            result.setData(list);
        } else {
            result.setStatus(ResultConstant.EMPTIY);
        }
        return result;
    }
}
